package com.ljw.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ljw.common.exception.BizCodeEnum;
import com.ljw.gulimall.member.exception.PhoneExistException;
import com.ljw.gulimall.member.exception.UsernameExistException;
import com.ljw.gulimall.member.vo.MemberLoginVo;
import com.ljw.gulimall.member.vo.MemberLoginWxVo;
import com.ljw.gulimall.member.vo.MemberRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ljw.gulimall.member.entity.MemberEntity;
import com.ljw.gulimall.member.service.MemberService;
import com.ljw.common.utils.PageUtils;
import com.ljw.common.utils.R;

import javax.servlet.http.HttpSession;


/**
 * 会员
 *
 * @author molimicha
 * @email 1328388970@qq.com
 * @date 2022-08-07 03:52:36
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }

    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo memberRegistVo){

        try {
            memberService.regist(memberRegistVo);
        }catch (PhoneExistException exception){
            R error = R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMessage());
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMessage());
        }catch (UsernameExistException exception){
            R error = R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), BizCodeEnum.USER_EXIST_EXCEPTION.getMessage());
            return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), BizCodeEnum.USER_EXIST_EXCEPTION.getMessage());
        }

        return R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLoginVo){
        MemberEntity memberEntity = memberService.login(memberLoginVo);
        if (memberEntity == null){
            return R.error(BizCodeEnum.LOGINCATT_PASSWORD_INVAILD_EXCEPTION.getCode(), BizCodeEnum.LOGINCATT_PASSWORD_INVAILD_EXCEPTION.getMessage());
        }
        return R.ok().put("memberInfo", memberEntity);
    }

    @PostMapping("/wxLogin")
    public R wxLogin(@RequestBody MemberLoginWxVo memberLoginWxVo){
        MemberEntity memberEntity = memberService.wxLogin(memberLoginWxVo);
        return R.ok().put("data", memberEntity);
    }

//    @GetMapping("/testSession")
//    public R testSession(HttpSession session){
//        String test = (String) session.getAttribute("test");
//        System.out.println(test);
//        return R.ok();
//    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);
        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
